﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using External.Controls.Controls;
using External.Controls.Interfaces;
using External.Core;
using Rectangle = System.Windows.Shapes.Rectangle;

namespace External.Controls.ModelViews
{
    public class SectionViewModel : IDrawable, IDrawableScheme
    {
        public SectionViewModel(HallViewModel hall, Section section)
        {
            Hall = hall;
            Section = section;
        }

        /// <summary>
        /// Контроллер зала
        /// </summary>
        public HallViewModel Hall { get; set; }

        /// <summary>
        /// Оригинальная модель данных секции
        /// </summary>
        public Section Section { get; set; }

        /// <summary>
        /// То как секция отображает себя, на какой либо схеме
        /// </summary>
        public FrameworkElement Geometry
        {
            get
            {
                var sectionPoints = Section.Points;

                Path path = new Path();
                path.Stroke = Brushes.Black;
                path.Fill = Section.Brush;

                var points = sectionPoints.Skip(1).ToList();
                List<PathSegment> segments = new List<PathSegment>();
                for (int i = 0; i < points.Count(); i++)
                    segments.Add(new LineSegment(points[i], true));

                IList<PathFigure> en = new List<PathFigure>();
                en.Add(new PathFigure(sectionPoints.First(), segments, true));

                path.Data = new PathGeometry(en);

                double w = sectionPoints.Max(x => x.X) - sectionPoints.Min(x => x.X);
                double h = sectionPoints.Max(x => x.Y) - sectionPoints.Min(x => x.Y);

                path.Width = w;
                path.Height = h;

                DesignerItem item = new DesignerItem();
                item.DataContext = this;
                item.Content = path;

                HallCanvas.SetLeft(item, Section.Left);
                HallCanvas.SetTop(item, Section.Top);

                return item;
            }
        }

        /// <summary>
        /// То как секция отображается фигуры будучи самой схемой
        /// </summary>
        public IList<FrameworkElement> Geometries
        {
            get
            {
                var elements = Section.Seats.Select(x => new SeatViewModel(x)).Select(x => x.Geometry).ToList();

                #region ЗАМЕНЮ\Уберу
                ResourceDictionary dictionary = new ResourceDictionary();
                dictionary.Source = new Uri("../Resources/Styles/Resources.xaml", UriKind.RelativeOrAbsolute);

                //Имя сеанса
                TextBlock titleSection = new TextBlock();
                titleSection.Text = "Схема секции " + Section.Name;
                titleSection.FontSize = 40;
                titleSection.FontWeight = FontWeights.Bold;
                HallCanvas.SetLeft(titleSection, 700);
                HallCanvas.SetTop(titleSection, 20);
                elements.Add(titleSection);

                //Ссылка обратно
                TextBlock hyperlink = (TextBlock)dictionary["LinkBack"];
                hyperlink.DataContext = this;
                HallCanvas.SetLeft(hyperlink, 10);
                HallCanvas.SetTop(hyperlink, 10);
                elements.Add(hyperlink);
                #endregion
                
                //Всякая декорация
                foreach (var obj in Section.Decorations)
                {
                    if (obj is Core.Rectangle)
                    {
                        Core.Rectangle rect = (Core.Rectangle)obj;
                        Rectangle rectangle = new Rectangle();
                        HallCanvas.SetLeft(hyperlink, rect.Left);
                        HallCanvas.SetTop(hyperlink, rect.Top);
                        rectangle.Width = rect.Width;
                        rectangle.Height = rect.Height;

                        elements.Add(rectangle);
                    }

                    if (obj is Core.Ellipse)
                    {
                        Core.Ellipse rect = (Core.Ellipse)obj;
                        Rectangle rectangle = new Rectangle();
                        HallCanvas.SetLeft(hyperlink, rect.Left);
                        HallCanvas.SetTop(hyperlink, rect.Top);
                        rectangle.Width = rect.Width;
                        rectangle.Height = rect.Height;

                        elements.Add(rectangle);
                    }
                }

                Canvas legendBoard = (Canvas)dictionary["SectionLegendBoard"];
                HallCanvas.SetLeft(legendBoard, 10);
                HallCanvas.SetTop(legendBoard, 60);
                
                elements.Add(legendBoard);

                return elements;
            }
        }
    }
}